package org.ofbiz.sys.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.ofbiz.base.util.UtilValidate;

import javolution.util.FastList;

public class CommonUtils {
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map parametersToInputFields(Map<String, ? extends Object> parameters) {
        
		Set<String> processed = new LinkedHashSet<String>();
        Map result = new HashMap();
        Set<String> keys = new LinkedHashSet<String>();
        
        for (Map.Entry<String, ? extends Object> entry : parameters.entrySet()) {
            String parameterName = entry.getKey();
	            if((!parameterName.equals("pageNo"))&&(!parameterName.equals("pageSize"))&&(!parameterName.equals("tbName"))
	            	&&(!parameterName.equals("orderBy"))&&(!parameterName.equals("timeZone"))&&(!parameterName.equals("locale"))
	            	&&(!parameterName.equals("userLogin"))) {
	            	result.put(parameterName,parameters.get(parameterName) );
	            	if (parameterName.endsWith("_op")) {
	            		String fieldName = parameterName.substring(0, parameterName.length() - 3);
	            		processed.add(fieldName);
		            }else {
		            	keys.add(parameterName);
		            }
	            }
            }
        
        Set<String> resultKeys = new LinkedHashSet<String>();
        for(String key:keys) {
        	if (!processed.contains(key)) {
        		resultKeys.add(key);
            }
        }
        for(String key:resultKeys) { 
        	result.put(key+"_op","contains");
        }
        return result;
   }

	@SuppressWarnings("rawtypes")
	public static List splitStringToList(String inputString) {
		return splitCustomStringToList(inputString, ";");
	}
	/**
	 * 分割字符串 自定义的分隔符
	 * 
	 * @param inputString
	 * @param splitChar
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List splitCustomStringToList(String inputString, String splitChar) {
		List result = FastList.newInstance();
		if (UtilValidate.isEmpty(inputString)) {
			return result;
		}
		inputString =inputString.trim();
		String[] array = inputString.split(splitChar);
		for (int i = 0; i < array.length; i++) {
			if (UtilValidate.isNotEmpty(array[i])) {
				result.add(array[i]);
			}
		}
		return result;
	}
	public static String subStringToNewStr(String inputString, Integer num1,Integer num2) {
		inputString = inputString.substring(num1,num2);
		return inputString;
	}
	
	
	public static int getRandom() {
		int s = (int)(1+Math.random()*(100-1+1));
		return s;
	}
	
	public static void main(String[] args){
		System.out.println(getRandom());
	}
	 /** 
     * 获取上一个月 
     *  
     * @return 
     */  
    public static String getLastMonth() {  
        Calendar cal = Calendar.getInstance();  
        cal.add(Calendar.MONTH, -1);  
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
        String lastMonth = dft.format(cal.getTime());  
        return lastMonth;  
    } 
    
	 /** 
     * 获取上几天
     *  
     * @return 
     */  
    public static Timestamp getLastDay(Timestamp date , Long day) {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);
        cal.add(Calendar.DATE, (int)(day*1));  
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
        String lastMonth = dft.format(cal.getTime());  
        Timestamp newTime = Timestamp.valueOf(lastMonth);
        return newTime;  
    } 
}
